<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>属性管理</el-breadcrumb-item>
    </el-breadcrumb>

    <h3 style="margin: 20px 0;">属性管理</h3>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="templateId" label="所属模版" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" align="center"></el-table-column>
      <el-table-column label="类别" align="center">
        <template slot-scope="scope">
          <span v-text="typeText[scope.row.type]"></span>
        </template>
      </el-table-column>
      <el-table-column label="输入类型" align="center">
        <template slot-scope="scope">
          <span v-text="inputTypeText[scope.row.inputType]"></span>
        </template>
      </el-table-column>
      <el-table-column prop="valueList" label="备选值列表" align="center"></el-table-column>
      <el-table-column prop="unit" label="单位" align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" align="center"></el-table-column>
      <el-table-column label="是否允许自定义" align="center">
        <template slot-scope="scope">
          <span v-text="scope.row.isAllowCustomize == 1 ? '是' : '否'"></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button circle icon="el-icon-edit" size="mini" @click="handleEdit(scope.row.id)"></el-button>
          <el-button circle icon="el-icon-delete" size="mini" type="danger"
                     @click="openDeleteConfirm(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        typeText: ['销售属性', '非销售属性'],
        inputTypeText: ['手动录入', '单选', '多选', '单选（下拉列表）', '多选（下拉列表）'],
        tableData: []
      }
    },
    methods: {
      openDeleteConfirm(id) {
        this.$confirm('此操作将永久删除该属性, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(id);
        }).catch(() => {
        });
      },
      handleEdit(id) {
        console.log('将编辑id=' + id + "的属性数据……");
        this.$message.error('尝试编辑id=' + id + '的属性数据，但是，此功能尚未实现！');
      },
      handleDelete(id) {
        console.log('将删除id=' + id + "的属性数据……");
        let url = 'http://localhost:9080/attributes/' + id + '/delete';
        this.axios
                .create({headers: {'Authorization': localStorage.getItem('jwt')}})
                .post(url).then((response) => {
          let json = response.data;
          if (json.code == 20000) {
            this.$message({
              message: '删除属性成功！',
              type: 'success'
            });
          } else {
            this.$message.error(response.data.message);
          }
          this.loadAttributes();
        }).catch(() => {
          // 执行到此处，通常是没有权限，例如没有携带JWT，被服务器端视为未登录，或当前登录的账号确实无权限
          this.$message.error('删除属性失败，服务器忙，请稍后再次尝试！');
        });
      },
      handleChangeEnable(i, id, toState) {
        let enableText = ['禁用', '启用'];
        let originState = (toState + 1) % 2;
        this.$message.error('尝试将id=' + id + '的属性状态从【' + enableText[originState] + '】改为【' + enableText[toState] + '】，但是，此功能尚未实现！');
        setTimeout(() => {
          this.tableData[i].enable = originState;
        }, 200);
      },
      loadAttributes() {
        console.log('loadAlbums()');
        let url = 'http://localhost:9080/attributes';
        this.axios
                .create({headers: {'Authorization': localStorage.getItem('jwt')}})
                .get(url).then((response) => {
          let json = response.data;
          if (json.code == 20000) {
            this.tableData = json.data;
          } else {
            this.$message.error(response.data.message);
          }
        });
      }
    },
    mounted() {
      this.loadAttributes();
    }
  }
</script>

<style scoped>

</style>